package src.project.numbers;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class numbers extends Activity implements OnSharedPreferenceChangeListener {
	
	private TableLayout table;
	private int to;
	private int from;
	private TextView toBox;
	private TextView fromBox;
	private SharedPreferences preferences;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("to", "10");
        editor.putString("from", "1");
        editor.commit();
        
        preferences.registerOnSharedPreferenceChangeListener(this);
        
        table = (TableLayout)findViewById(R.id.table1);
        toBox = (TextView)findViewById(R.id.to_num);
        fromBox = (TextView)findViewById(R.id.from_num);
        
        String to_string = preferences.getString("to", "10");
        String from_string = preferences.getString("from", "1");
        
        toBox.setText(to_string);
        fromBox.setText(from_string);
        
        to = Integer.parseInt(to_string);
        from = Integer.parseInt(from_string);
        LoadValues();
    }
    
    private void LoadValues(){
        for(int i = from; i <= to; i++)
        {
        	TableRow row = new TableRow(this);
        	
        	TextView base = new  TextView(this);
        	base.setGravity(Gravity.CENTER_HORIZONTAL);
        	base.setText(String.valueOf(i));
        	base.setTextSize(16f);
        	base.setTextColor(R.color.BaseColor);
        	base.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, 
        												   LayoutParams.WRAP_CONTENT, 
        												   1f));
        	row.addView(base);
        	
        	TextView squared = new TextView(this);
        	squared.setGravity(Gravity.CENTER_HORIZONTAL);
        	squared.setText(String.valueOf(i*i));
        	squared.setTextSize(16f);
        	squared.setTextColor(R.color.SquareColor);
        	squared.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, 
        													  LayoutParams.WRAP_CONTENT, 
        													  1f));
        	row.addView(squared);
        	
        	TextView cubed = new TextView(this);
        	cubed.setGravity(Gravity.CENTER_HORIZONTAL);
        	cubed.setText(String.valueOf(i*i*i));
        	cubed.setTextSize(16f);
        	cubed.setTextColor(R.color.CubedColor);
        	cubed.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, 
        													LayoutParams.WRAP_CONTENT, 
        													1f));
        	row.addView(cubed);

        	table.addView(row);
        }
    }
    
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflator = getMenuInflater();
    	inflator.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch(item.getItemId()){
    	case R.id.Prefs:
    		Intent i = new Intent(this, Preferences.class);
    		startActivity(i);
    		break;
    	}
    	return true;
    }

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key) {
		
		String to_string = sharedPrefs.getString("to", "10");
		String from_string = sharedPrefs.getString("from", "1");
		
		toBox.setText(to_string);
		fromBox.setText(from_string);
		
		to = Integer.parseInt(to_string);
		from = Integer.parseInt(from_string);
		
		if (Math.abs(to-from) > 250){
			to = from + 250;
			toBox.setText(String.valueOf(to));
		}
		
		TableRow beginning = (TableRow)findViewById(R.id.row1);
		
		table.removeAllViews();
		table.addView(beginning);
		
		LoadValues();
	}
}